package com.ningmengs789.live.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.ningmengs789.live.R;
import com.ningmengs789.live.net.response.ReasonListResponse;

public class ReportAdapter extends RecyclerView.Adapter<ReportAdapter.Vh> {

    private ReasonListResponse response;

    private LayoutInflater mInflater;

    public ReportAdapter(Context context){
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public Vh onCreateViewHolder(ViewGroup parent, int viewType) {
        return new Vh(mInflater.inflate(R.layout.item_report_cb, parent, false));
    }

    @Override
    public void onBindViewHolder(Vh vh, int position) {
        vh.setData(response.getList().get(position));
    }

    @Override
    public int getItemCount() {
        if (response == null){
            return 0;
        }
        return response.getList().size();
    }

    private MyItemClickListener mOnItemClickListener;

    public interface MyItemClickListener {
        void onCheckedChanged(CompoundButton buttonView, boolean isChecked, int position);
    }

    public void setOnItemClickListener(MyItemClickListener listener){
        this.mOnItemClickListener = listener;
    }

    public void setCards(ReasonListResponse response) {
        if (response == null) {
            return;
        }
        this.response = response;
    }

    class Vh extends RecyclerView.ViewHolder {

        CheckBox mCheckBox;

        public Vh(View itemView) {
            super(itemView);
            mCheckBox = (CheckBox) itemView.findViewById(R.id.item_report_cb);

            mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    mOnItemClickListener.onCheckedChanged(buttonView, isChecked, getLayoutPosition());
                }
            });
        }

        void setData(ReasonListResponse.ListBean bean) {
            itemView.setTag(bean.getReason_id());
            mCheckBox.setText(bean.getReason());
        }
    }

}
